package cn.com.suiyi.shixingshengxian.main;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.suiyi.shixingshengxian.R;
import cn.com.suiyi.shixingshengxian.active.ActiveFragment;
import cn.com.suiyi.shixingshengxian.base.BaseActivity;
import cn.com.suiyi.shixingshengxian.classify.ClassifyFragment;
import cn.com.suiyi.shixingshengxian.home.HomeFragment;
import cn.com.suiyi.shixingshengxian.mine.MineFragment;
import cn.com.suiyi.shixingshengxian.shopping.ShoppingFragment;
import cn.com.suiyi.shixingshengxian.utils.BottomTabManager;

public class MainActivity extends BaseActivity implements Toolbar.OnMenuItemClickListener {
    private static final int HOME = 0;
    private static final int ACTIVE = 1;
    private static final int CLASSIFY = 2;
    private static final int SHOPPING = 3;
    private static final int MINE = 4;
    @InjectView(R.id.toolbar_main)
    Toolbar toolbarMain;
    @InjectView(R.id.imageview_home_checked)
    ImageView imageviewHomeChecked;
    @InjectView(R.id.imageview_home_unchecked)
    ImageView imageviewHomeUnchecked;
    @InjectView(R.id.layout_home_main)
    RelativeLayout layoutHomeMain;
    @InjectView(R.id.imageview_active_checked)
    ImageView imageviewActiveChecked;
    @InjectView(R.id.imageview_active_unchecked)
    ImageView imageviewActiveUnchecked;
    @InjectView(R.id.layout_active_main)
    RelativeLayout layoutActiveMain;
    @InjectView(R.id.imageview_classify_checked)
    ImageView imageviewClassifyChecked;
    @InjectView(R.id.imageview_classify_unchecked)
    ImageView imageviewClassifyUnchecked;
    @InjectView(R.id.layout_classify_main)
    RelativeLayout layoutClassifyMain;
    @InjectView(R.id.imageview_shopping_checked)
    ImageView imageviewShoppingChecked;
    @InjectView(R.id.imageview_shopping_unchecked)
    ImageView imageviewShoppingUnchecked;
    @InjectView(R.id.layout_shop_main)
    RelativeLayout layoutShopMain;
    @InjectView(R.id.imageview_mine_checked)
    ImageView imageviewMineChecked;
    @InjectView(R.id.imageview_mine_unchecked)
    ImageView imageviewMineUnchecked;
    @InjectView(R.id.layout_mine_main)
    RelativeLayout layoutMineMain;
    @InjectView(R.id.lineatlayout_navigation)
    LinearLayout lineatlayoutNavigation;
    @InjectView(R.id.framelayout)
    FrameLayout framelayout;
    @InjectView(R.id.textview_home_main)
    TextView textviewHomeMain;
    @InjectView(R.id.textview_active_main)
    TextView textviewActiveMain;
    @InjectView(R.id.textview_classify_main)
    TextView textviewClassifyMain;
    @InjectView(R.id.textview_shopping_main)
    TextView textviewShoppingMain;
    @InjectView(R.id.textview_mine_main)
    TextView textviewMineMain;
    @InjectView(R.id.imageview_toolbar_main)
    ImageView imageviewToolbarMain;
    @InjectView(R.id.textview_toolbar_main)
    TextView textviewToolbarMain;
    private FragmentManager manager;
    private HomeFragment mHomeFragment;
    private ActiveFragment mActiveFragment;
    private ClassifyFragment mClassifyFragment;
    private ShoppingFragment mShoppingFragment;
    private MineFragment mMineFragment;
    private List<Fragment> fragments;
    private BottomTabManager bottomTabManager;


    @Override
    protected void initData() {

    }

    @Override
    public int getLayout() {
        return R.layout.activity_main;
    }


    @Override
    public void initView() {
        fragments = new ArrayList<>();
        manager = getSupportFragmentManager();
        mHomeFragment = new HomeFragment();
        mActiveFragment = new ActiveFragment();
        mClassifyFragment = new ClassifyFragment();
        mShoppingFragment = new ShoppingFragment();
        mMineFragment = new MineFragment();
        fragments.add(mHomeFragment);
        fragments.add(mActiveFragment);
        fragments.add(mClassifyFragment);
        fragments.add(mShoppingFragment);
        fragments.add(mMineFragment);
        bottomTabManager = new BottomTabManager(fragments, R.id.framelayout, manager);
        bottomTabManager.setSelection(HOME);
        bottomTabManager.setListener(new BottomTabManager.OnBottomTabSelectListener() {
            @Override
            public void onBottomTabSelectListener(int index, int count) {
                if (toolbarMain.getMenu() != null) {
                    toolbarMain.getMenu().clear();
                }
                imageviewToolbarMain.setVisibility(View.INVISIBLE);
//                textviewToolbarMain.setText("");
                switch (index) {
                    case 0:
                        toolbarMain.inflateMenu(R.menu.main_home);
                        imageviewToolbarMain.setVisibility(View.VISIBLE);
                        textviewToolbarMain.setVisibility(View.VISIBLE);
                        break;
                    case 1:
                        textviewToolbarMain.setText("活动");
                        break;
                    case 2:
                        toolbarMain.inflateMenu(R.menu.main_home);
                        textviewToolbarMain.setText("分类");
                        break;
                    case 3:
                        textviewToolbarMain.setText("购物车");
                        toolbarMain.inflateMenu(R.menu.main_shopping);
                        break;
                    case 4:
                        textviewToolbarMain.setText("我的食行");
                        toolbarMain.inflateMenu(R.menu.main_mine);
                        break;
                }
                textviewToolbarMain.setVisibility(View.VISIBLE);
            }
        });
    }


    @OnClick({R.id.layout_home_main, R.id.layout_active_main, R.id.layout_classify_main, R.id.layout_shop_main, R.id.layout_mine_main})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_home_main:
                if (imageviewHomeChecked.getVisibility() == View.VISIBLE) {
                    return;
                }
                if (imageviewHomeChecked.getVisibility() == View.INVISIBLE) {
                    bottomTabManager.setSelection(HOME);
                    inVisibility();
                    imageviewHomeUnchecked.setVisibility(View.INVISIBLE);
                    imageviewHomeChecked.setVisibility(View.VISIBLE);
                    textviewHomeMain.setTextColor(Color.GREEN);

                }
                break;
            case R.id.layout_active_main:
                if (imageviewActiveChecked.getVisibility() == View.VISIBLE) {
                    return;
                }
                if (imageviewActiveChecked.getVisibility() == View.INVISIBLE) {
                    bottomTabManager.setSelection(ACTIVE);
                    inVisibility();
                    imageviewActiveUnchecked.setVisibility(View.INVISIBLE);
                    imageviewActiveChecked.setVisibility(View.VISIBLE);
                    textviewActiveMain.setTextColor(Color.GREEN);
                }
                break;
            case R.id.layout_classify_main:
                if (imageviewClassifyChecked.getVisibility() == View.VISIBLE) {
                    return;
                }
                if (imageviewClassifyChecked.getVisibility() == View.INVISIBLE) {
                    bottomTabManager.setSelection(CLASSIFY);
                    inVisibility();
                    imageviewClassifyUnchecked.setVisibility(View.INVISIBLE);
                    imageviewClassifyChecked.setVisibility(View.VISIBLE);
                    textviewClassifyMain.setTextColor(Color.GREEN);
                }
                break;
            case R.id.layout_shop_main:
                if (imageviewShoppingChecked.getVisibility() == View.VISIBLE) {
                    return;
                }
                if (imageviewShoppingChecked.getVisibility() == View.INVISIBLE) {
                    bottomTabManager.setSelection(SHOPPING);
                    inVisibility();
                    imageviewShoppingUnchecked.setVisibility(View.INVISIBLE);
                    imageviewShoppingChecked.setVisibility(View.VISIBLE);
                    textviewShoppingMain.setTextColor(Color.GREEN);
                }
                break;
            case R.id.layout_mine_main:
                if (imageviewMineChecked.getVisibility() == View.VISIBLE) {
                    return;
                }
                if (imageviewMineChecked.getVisibility() == View.INVISIBLE) {
                    bottomTabManager.setSelection(MINE);
                    inVisibility();
                    imageviewMineUnchecked.setVisibility(View.INVISIBLE);
                    imageviewMineChecked.setVisibility(View.VISIBLE);
                    textviewMineMain.setTextColor(Color.GREEN);
                }
                break;
        }
    }

    /**
     * 全部隐藏
     */
    public void inVisibility() {

        List<ImageView> checkedViews = new ArrayList<>();
        checkedViews.add(imageviewHomeChecked);
        checkedViews.add(imageviewActiveChecked);
        checkedViews.add(imageviewClassifyChecked);
        checkedViews.add(imageviewShoppingChecked);
        checkedViews.add(imageviewMineChecked);
        List<TextView> textViews = new ArrayList<>();
        textViews.add(textviewHomeMain);
        textViews.add(textviewActiveMain);
        textViews.add(textviewClassifyMain);
        textViews.add(textviewShoppingMain);
        textViews.add(textviewMineMain);
        List<ImageView> unCheckedViews = new ArrayList<>();
        unCheckedViews.add(imageviewHomeUnchecked);
        unCheckedViews.add(imageviewActiveUnchecked);
        unCheckedViews.add(imageviewClassifyUnchecked);
        unCheckedViews.add(imageviewShoppingUnchecked);
        unCheckedViews.add(imageviewMineUnchecked);
        for (int i = 0; i < checkedViews.size(); i++) {
            checkedViews.get(i).setVisibility(View.INVISIBLE);
            textViews.get(i).setTextColor(Color.BLACK);
            unCheckedViews.get(i).setVisibility(View.VISIBLE);
        }
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        return false;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }
}
